package com.ynkbny.web.controller;

import com.ynkbny.base.PageResult;
import com.ynkbny.domain.DataCodeAnalyzeMapping;
import com.ynkbny.domain.dto.DataCodeAnalyzeMappingDTO;
import com.ynkbny.service.DatacodeAnalyzeMappingService;
import com.ynkbny.util.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 杨亚
 * @version 1.0.0
 * @title DataCodeAnalyzeMappingController
 * @description <ToDo description class purpose>
 * @create 2023/11/20 17:12
 **/
@Slf4j
@Api(tags = "采集配置项与老狗平台分析项映射接口")
@RestController
@RequestMapping("/datacodeanalyzemapping")
public class DataCodeAnalyzeMappingController {

    @Resource
    private DatacodeAnalyzeMappingService datacodeAnalyzeMappingService;

    @ApiOperation("保存映射")
    @PostMapping("/save")
    public Res<Boolean> save(@RequestBody DataCodeAnalyzeMappingDTO datacodeAnalyzeMappingDTO) {
        // 创建一个DataCodeAnalyzeMapping对象
        DataCodeAnalyzeMapping datacodeAnalyzeMapping = new DataCodeAnalyzeMapping();
        // 使用BeanUtils工具类将DataCodeAnalyzeMappingDTO对象属性复制到DataCodeAnalyzeMapping对象中
        BeanUtils.copyProperties(datacodeAnalyzeMappingDTO, datacodeAnalyzeMapping);
        // 如果DataCodeAnalyzeMapping对象为空，返回false
        if (ObjectUtils.isEmpty(datacodeAnalyzeMapping)) {
            return Res.fail(Boolean.FALSE);
        }
        // 调用datacodeAnalyzeMappingService的saveOrUpdate方法，保存DataCodeAnalyzeMapping对象
        boolean save = datacodeAnalyzeMappingService.saveOrUpdate(datacodeAnalyzeMapping);
        // 返回保存结果
        return Res.ok(save);
    }

    @ApiOperation("删除映射")
    @PostMapping("/delete")
    public Res<Boolean> delete(@RequestBody DataCodeAnalyzeMapping datacodeAnalyzeMapping) {
        // 判断参数是否为空
        if (ObjectUtils.isEmpty(datacodeAnalyzeMapping) && datacodeAnalyzeMapping.getId() == null) {
            return Res.fail(Boolean.FALSE);
        }
        // 删除映射
        boolean delete = datacodeAnalyzeMappingService.removeById(datacodeAnalyzeMapping);
        return Res.ok(delete);
    }

    @ApiOperation("更新映射")
    @PostMapping("/update")
    public Res<Boolean> update(@RequestBody DataCodeAnalyzeMapping datacodeAnalyzeMapping) {
        // 更新映射
        return Res.ok(datacodeAnalyzeMappingService.updateById(datacodeAnalyzeMapping));
    }

    @ApiOperation("映射信息分页查询")
    @PostMapping("/pageList")
    public Res<PageResult<DataCodeAnalyzeMapping>> pageList(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                            @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize,
                                                            @RequestParam(name = "condition", required = false) String conditionJson) {
        // 分页查询映射信息
        return Res.ok(datacodeAnalyzeMappingService.pageList(conditionJson, pageNum, pageSize));
    }

    @ApiOperation("根据id查询映射信息")
    @GetMapping("/findById")
    public Res<DataCodeAnalyzeMappingDTO> findById(@RequestParam(name = "id") String id) {
        // 根据id查询映射信息
        return Res.ok(datacodeAnalyzeMappingService.findById(id));
    }
}
